# set working directory to location of the R file
#setwd("replication/scripts")

require(devtools)
# uncomment if not installed in previous file:
#devtools::install_github("ChandlerLutz/starpolishr")
library(starpolishr)

require(pacman)
pacman::p_load(tidyverse, lfe, dplyr, ggplot2, ggpubr, stargazer,
               purrr, cowplot, starpolishr, here)

# All models run using PanelMatch version 1.0.0

require(devtools)
# uncomment if not installed in previous file:
#install_version("PanelMatch", version = "1.0.0", repos = "http://cran.us.r-project.org")

library(PanelMatch)

# # revenue share HHI
# load("C:/Users/bcke.egb/Dropbox (CBS)/Trump_and_the_Beachheads/Data/firm_panel_revenue_distributions.RData")
# 
# # combined panel
# load("C:/Users/bcke.egb/Dropbox (CBS)/Trump_and_the_Beachheads/Data/combined_panel.RData")

load("../Data/firm_panel_revenue_distributions.RData")

# combined panel
load("../Data/combined_panel.RData")


# issues panel
load("../Data/firm_panel_issues.RData")



combined_panel <- as.data.frame(combined_panel)
combined_panel$period <- as.integer(combined_panel$period)
combined_panel$log_rev <- log(combined_panel$revenue)

# check staff and bureau covariates are the same
combined_panel$log_contracts <- log(combined_panel$total_bur_filings)
combined_panel$log_lobby <- log(combined_panel$total_bur_lobbyists)

# create treatment indicator
combined_panel$treat <- ifelse(combined_panel$prev_exp == 1 | combined_panel$staff_exp == 1, 1, 0)
combined_panel$treat <- ifelse(is.na(combined_panel$treat)==T,0,combined_panel$treat)

combined_panel <- left_join(combined_panel, firm_panel_revenue_dist[,c(1,2,8)])

# NAs are missing issue areas in the report; need to drop or otherwise infer
firm_panel_issues <- firm_panel_issues %>% 
  ungroup %>% 
  select(reg_lobby_core_id, period, total_issues:issues_per_client)

combined_panel <- left_join(combined_panel, firm_panel_issues)



#############
# convenience functions

# function for extracting id of control firms

extract_control <- function(cont, pm_obj){
  
  crnt_control <- as.numeric(as.character(pm_obj$att[[cont]]))
  return(crnt_control)
}

# wrapper function for PanelMatch

pm_wrap <- function(lags, dataset, treat, refine = F,
                    DV){
  
  if(refine == F){
    comb <- PanelMatch(lag = lags, 
                       time.id = "period", unit.id = "reg_lobby_core_id", 
                       treatment = treat, 
                       refinement.method = "none", 
                       data = dataset, 
                       match.missing = T, 
                       qoi = "att" ,
                       outcome.var = DV,
                       lead = 0:4, 
                       forbid.treatment.reversal = T)
    
    dist_control <- comb$att
    comb_obs <- sum(print(comb$att)[,3])
    
    if(nchar(comb_obs) == 6){
      comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
                        substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
                          comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                                            substr(comb_obs, 3, nchar(comb_obs)), sep = "")
                        }
    
    unique_treat <- length(unique(print(comb$att)[,1]))
    
    map_length <- 1:length(print(comb$att)[,1])
    unique_control <- purrr::map(map_length,
                                 ~ extract_control(cont = ., pm_obj = comb))
    
    unique_control <- do.call("rbind", unique_control)
    unique_control <- length(unique(unique_control))
    
    unique_control <- paste(substr(unique_control, 1, 2), ",", 
                            substr(unique_control, 3, nchar(unique_control)), sep = "")
    
  }else{comb <- PanelMatch(lag = lags, 
                           time.id = "period", unit.id = "reg_lobby_core_id", 
                           treatment = treat, 
                           refinement.method = "mahalanobis", #alternative none
                           data = dataset, 
                           match.missing = T, 
                           covs.formula = ~ I(lag(log_contracts,1:4)) + I(lag(log_lobby,1:4)) + I(lag(log_rev,1:4)), 
                           qoi = "att" ,
                           outcome.var = DV,
                           lead = 0:4, 
                           forbid.treatment.reversal = T)
  dist_control <- comb$att
  comb_obs <- sum(print(comb$att)[,3])
  
  if(nchar(comb_obs) == 6){
    comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
                      substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
                        comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                                          substr(comb_obs, 3, nchar(comb_obs)), sep = "")
                      }
  
  unique_treat <- length(unique(print(comb$att)[,1]))
  
  map_length <- 1:length(print(comb$att)[,1])
  unique_control <- purrr::map(map_length,
                               ~ extract_control(cont = ., pm_obj = comb))
  
  unique_control <- do.call("rbind", unique_control)
  unique_control <- length(unique(unique_control))
  
  unique_control <- paste(substr(unique_control, 1, 2), ",", 
                          substr(unique_control, 3, nchar(unique_control)), sep = "")
  
  
  }
  
  comb_results <- PanelEstimate(sets = comb, 
                                data = dataset, 
                                confidence.level = .9)
  
  pm_res <- as.data.frame(summary(comb_results)[[1]][1:5, 1:2])
  
  
  return(list(res = pm_res, 
              n_control = comb_obs, 
              n_treat = unique_treat,
              n_unique_control = unique_control,
              dist_control = dist_control))
  
}


#-------------------------------------------------------
# estimate models

combined_panel$log_hhi <- log(combined_panel$revenue_hhi)
combined_panel$log_hhi[is.na(combined_panel$log_hhi)] <- 0
combined_panel$period <- as.integer(combined_panel$period)

#w/o adjustment
comb <- pm_wrap(lags = 6, dataset = combined_panel,
                treat = "treat", refine = F, DV = "log_hhi")


# With adjustment
comb2 <- pm_wrap(lags = 6, dataset = combined_panel,
                 treat = "treat", refine = T, DV = "log_hhi")

# helper object for the table
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

# stargazer
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb$res[,1], se = comb$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "Client Concentration (ln HHI)", 
                  title = c("Lobbyists Turning Government Employee and Lobby Firm Revenue"), 
                  label = c("hhi"),
                  add.lines = list(c("Treatment Events", comb$n_treat, comb$n_treat, comb$n_treat,
                                     comb$n_treat, comb$n_treat),
                                   c("Control Firms", comb$n_control, comb$n_control,
                                     comb$n_control, comb$n_control, comb$n_control),
                                   c("Unique Controls", comb$n_unique_control, comb$n_unique_control,
                                     comb$n_unique_control, comb$n_unique_control, comb$n_unique_control)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb2$res[,1], se = comb2$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "Client Concentration (ln HHI)", 
                  title = c("Lobbyists Turning Government Employees and Lobby Firm Revenue"), 
                  label = c("hhi"),
                  add.lines = list(c("Treatment Events", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Control Firms", comb2$n_control, comb2$n_control,
                                     comb2$n_control, comb2$n_control, comb2$n_control),
                                   c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                                     comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

# put combined table together
BothPanels <- star_panel(pan1, pan2, panel.names = c("Unadjusted Estimates", "Adjusted Estimates"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. Future-treated firms and treatment-switchers are never included. In Panel B, adjustments to the control group are made by matching firms using the Mahalanobis distance calculated on logged firm revenue, logged number of contracts and logged number of lobbyists, all calculated in the pre-treatment windows. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BothPanels, 
               file = "../tables/Table4.tex")










# PM function for other adjustments ---------------------------------------


pm_wrap2 <- function(lags, dataset, treat, refine = F,
                    DV){
  
  if(refine == F){
    comb <- PanelMatch(lag = lags, 
                       time.id = "period", unit.id = "reg_lobby_core_id", 
                       treatment = treat, 
                       refinement.method = "none", 
                       data = dataset, 
                       match.missing = T, 
                       qoi = "att" ,
                       outcome.var = DV,
                       lead = 0:4, 
                       forbid.treatment.reversal = T)
    
    dist_control <- comb$att
    comb_obs <- sum(print(comb$att)[,3])
    
    if(nchar(comb_obs) == 6){
      comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
                        substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
                          comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                                            substr(comb_obs, 3, nchar(comb_obs)), sep = "")
                        }
    
    unique_treat <- length(unique(print(comb$att)[,1]))
    
    map_length <- 1:length(print(comb$att)[,1])
    unique_control <- purrr::map(map_length,
                                 ~ extract_control(cont = ., pm_obj = comb))
    
    unique_control <- do.call("rbind", unique_control)
    unique_control <- length(unique(unique_control))
    
    unique_control <- paste(substr(unique_control, 1, 2), ",", 
                            substr(unique_control, 3, nchar(unique_control)), sep = "")
    
  }else{comb <- PanelMatch(lag = lags, 
                           time.id = "period", unit.id = "reg_lobby_core_id", 
                           treatment = treat, 
                           refinement.method = "mahalanobis", #alternative none
                           data = dataset, 
                           match.missing = T, 
                           covs.formula = ~ I(lag(log_contracts,1:4)) + I(lag(log_lobby,1:4)) + I(lag(log_rev,1:4)) + I(lag(issues_per_client,1:4)), 
                           qoi = "att" ,
                           outcome.var = DV,
                           lead = 0:4, 
                           forbid.treatment.reversal = T)
  dist_control <- comb$att
  comb_obs <- sum(print(comb$att)[,3])
  
  if(nchar(comb_obs) == 6){
    comb_obs <- paste(substr(comb_obs, 1, 3), ",", 
                      substr(comb_obs, 4, nchar(comb_obs)), sep = "")}else{
                        comb_obs <- paste(substr(comb_obs, 1, 2), ",", 
                                          substr(comb_obs, 3, nchar(comb_obs)), sep = "")
                      }
  
  unique_treat <- length(unique(print(comb$att)[,1]))
  
  map_length <- 1:length(print(comb$att)[,1])
  unique_control <- purrr::map(map_length,
                               ~ extract_control(cont = ., pm_obj = comb))
  
  unique_control <- do.call("rbind", unique_control)
  unique_control <- length(unique(unique_control))
  
  unique_control <- paste(substr(unique_control, 1, 2), ",", 
                          substr(unique_control, 3, nchar(unique_control)), sep = "")
  
  
  }
  
  comb_results <- PanelEstimate(sets = comb, 
                                data = dataset, 
                                confidence.level = .9)
  
  pm_res <- as.data.frame(summary(comb_results)[[1]][1:5, 1:2])
  
  
  return(list(res = pm_res, 
              n_control = comb_obs, 
              n_treat = unique_treat,
              n_unique_control = unique_control,
              dist_control = dist_control))
  
}


#split sample by client and contract issue average

issue_firms  <- combined_panel %>%
  ungroup %>%
  filter(!is.na(issues_per_contract)) %>% 
  group_by(reg_lobby_core_id) %>% 
  summarize(mean_issues_client = mean(issues_per_client),
            mean_issues_contract = mean(issues_per_contract)) %>% 
  mutate(quartile_client = ntile(mean_issues_client, 4),
         quartile_contract = ntile(mean_issues_contract, 4))




comb <- pm_wrap(lags = 6, dataset = combined_panel %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client <= 2]),
                treat = "treat", refine = F, DV = "log_rev")

comb2 <- pm_wrap(lags = 6, dataset = combined_panel  %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client > 2]),
                treat = "treat", refine = F, DV = "log_rev")




# With adjustment
comb3 <- pm_wrap2(lags = 6, dataset = combined_panel,
                  treat = "treat", refine = T, DV = "log_rev")


# helper object for the table
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

# stargazer
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb$res[,1], se = comb$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  add.lines = list(c("Treatment Events (specialist)", comb$n_treat, comb$n_treat, comb$n_treat,
                                     comb$n_treat, comb$n_treat),
                                   c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                    comb2$n_treat, comb2$n_treat),
                                   c("Treatment Events (full sample)", comb3$n_treat, comb3$n_treat, comb3$n_treat,
                                     comb3$n_treat, comb3$n_treat)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb2$res[,1], se = comb2$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Control Firms", comb2$n_control, comb2$n_control,
                                     comb2$n_control, comb2$n_control, comb2$n_control),
                                   c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                                     comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

pan3 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb3$res[,1], se = comb3$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  # add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                  #                    comb2$n_treat, comb2$n_treat),
                  #                  c("Control Firms", comb2$n_control, comb2$n_control,
                  #                    comb2$n_control, comb2$n_control, comb2$n_control),
                  #                  c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                  #                    comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

# put combined table together
BothPanels <- star_panel(pan1, pan2, pan3, panel.names = c("Specialist Firms (issues per client)", "Generalist Firms (issues per client)", "Combined Adjusted Results"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. 
                             Future-treated firms and treatment-switchers are never included. Panel A includes firms that are below the 50th percentile of issues per client; Panel B includes firms above the 50th percentile of issues per client.
                             Panel C includes the full sample with full matching covariates, including issues per client. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BothPanels, 
               file = "../tables/TableD12.tex")

# 
# comb <- pm_wrap(lags = 6, dataset = combined_panel %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_contract <= 2]),
#                 treat = "treat", refine = F, DV = "log_rev")
# 
# comb2 <- pm_wrap(lags = 6, dataset = issue_panel  %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_contract > 2]),
#                  treat = "treat", refine = F, DV = "log_rev")




# using clients and clients/revenue as outcome ----------------------------


combined_panel$log_clients <- log(combined_panel$total_clients)

## revenue per client
combined_panel$rev_client <- combined_panel$revenue/combined_panel$total_clients
combined_panel$log_rev_client <- log(combined_panel$rev_client)

combined_panel <- as.data.frame(combined_panel)
combined_panel$period <- as.integer(combined_panel$period)


comb <- pm_wrap(lags = 6, dataset = combined_panel %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client <= 2]),
                treat = "treat", refine = F, DV = "log_clients")

comb2 <- pm_wrap(lags = 6, dataset = combined_panel  %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client > 2]),
                 treat = "treat", refine = F, DV = "log_clients")


# With adjustment
comb3 <- pm_wrap2(lags = 6, dataset = combined_panel,
                  treat = "treat", refine = T, DV = "log_clients")


# helper object for the table
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

# stargazer
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb$res[,1], se = comb$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and ln Clients: Firm Specialization"), 
                  label = c("issues_results"),
                  add.lines = list(c("Treatment Events (specialist)", comb$n_treat, comb$n_treat, comb$n_treat,
                                     comb$n_treat, comb$n_treat),
                                   c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Treatment Events (full sample)", comb3$n_treat, comb3$n_treat, comb3$n_treat,
                                     comb3$n_treat, comb3$n_treat)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb2$res[,1], se = comb2$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Control Firms", comb2$n_control, comb2$n_control,
                                     comb2$n_control, comb2$n_control, comb2$n_control),
                                   c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                                     comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

pan3 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb3$res[,1], se = comb3$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  # add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                  #                    comb2$n_treat, comb2$n_treat),
                  #                  c("Control Firms", comb2$n_control, comb2$n_control,
                  #                    comb2$n_control, comb2$n_control, comb2$n_control),
                  #                  c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                  #                    comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

# put combined table together
BothPanels <- star_panel(pan1, pan2, pan3, panel.names = c("Specialist Firms (issues per client)", "Generalist Firms (issues per client)", "Combined Adjusted Results"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. 
                             Future-treated firms and treatment-switchers are never included. Panel A includes firms that are below the 50th percentile of issues per client; Panel B includes firms above the 50th percentile of issues per client.
                             Panel C includes the full sample with full matching covariates, including issues per client. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")

# export
star_tex_write(BothPanels, 
               file = "../tables/TableD13.tex")



## rev/client

comb <- pm_wrap(lags = 6, dataset = combined_panel %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client <= 2]),
                treat = "treat", refine = F, DV = "log_rev_client")

comb2 <- pm_wrap(lags = 6, dataset = combined_panel  %>% filter(reg_lobby_core_id %in% issue_firms$reg_lobby_core_id[issue_firms$quartile_client > 2]),
                 treat = "treat", refine = F, DV = "log_rev_client")


# With adjustment
comb3 <- pm_wrap2(lags = 6, dataset = combined_panel,
                  treat = "treat", refine = T, DV = "log_rev_client")


# helper object for the table
mod <- felm(log_rev ~ treat
            |reg_lobby_core_id + period | 0|reg_lobby_core_id,
            data = combined_panel) 

# stargazer
pan1 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb$res[,1], se = comb$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and ln Revenue per Client: Firm Specialization"), 
                  label = c("specialization_rev_client"),
                  add.lines = list(c("Treatment Events (specialist)", comb$n_treat, comb$n_treat, comb$n_treat,
                                     comb$n_treat, comb$n_treat),
                                   c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Treatment Events (full sample)", comb3$n_treat, comb3$n_treat, comb3$n_treat,
                                     comb3$n_treat, comb3$n_treat)),
                  header = F)

#Panel B
pan2 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb2$res[,1], se = comb2$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                                     comb2$n_treat, comb2$n_treat),
                                   c("Control Firms", comb2$n_control, comb2$n_control,
                                     comb2$n_control, comb2$n_control, comb2$n_control),
                                   c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                                     comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

pan3 <- stargazer(mod, mod, mod, mod, mod, 
                  coef =  comb3$res[,1], se = comb3$res[,2],
                  covariate.labels = c("Lobbyist Becomes Government Employee"),
                  column.labels = c("t=0", "t+1", "t+2", "t+3", "t+4"), no.space = T,
                  omit.stat = c("f", "adj.rsq", "rsq", "ser", "n"),
                  dep.var.labels = "ln Revenue", 
                  title = c("Lobbyists Turning Government Employee and Firm Revenue: Firm Specialization"), 
                  label = c("issues_results"),
                  # add.lines = list(c("Treatment Events (generalist)", comb2$n_treat, comb2$n_treat, comb2$n_treat,
                  #                    comb2$n_treat, comb2$n_treat),
                  #                  c("Control Firms", comb2$n_control, comb2$n_control,
                  #                    comb2$n_control, comb2$n_control, comb2$n_control),
                  #                  c("Unique Controls", comb2$n_unique_control, comb2$n_unique_control,
                  #                    comb2$n_unique_control, comb2$n_unique_control, comb2$n_unique_control)),
                  header = F)

# put combined table together
BothPanels <- star_panel(pan1, pan2, pan3, panel.names = c("Specialist Firms (issues per client)", "Generalist Firms (issues per client)", "Combined Adjusted Results"))

# add a note
BothPanels <- star_notes_tex(BothPanels, note.type = "threeparttable",
                             note = "Estimates are the averages of all possible 2x2 differences-in-differences where control groups include firms with identical treatment histories prior to each respective treatment event. 
                             Future-treated firms and treatment-switchers are never included. Panel A includes firms that are below the 50th percentile of issues per client; Panel B includes firms above the 50th percentile of issues per client.
                             Panel C includes the full sample with full matching covariates, including issues per client. Weighted firm-blocked bootstrapped standard errors in parentheses. 1,000 trials used. *p$<$0.1; **p$<$0.05; ***p$<$0.01")


# export
star_tex_write(BothPanels, 
               file = "../tables/TableD14.tex")
